﻿using MediatR;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.MaterialRequisitionCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.Read.API.ApplicationRead.CommandHandler.MaterialRequisitionCommandHandler
{
    /// <summary>
    /// 显示物资领用命令处理器
    /// </summary>
    public class ShowMaterialRequisitionCommandHandler : IRequestHandler<ShowMaterialRequisitionCommand,ListMaterialRequisitionDto<MaterialRequisition>>
    {
        /// <summary>
        /// 物资领用仓储接口
        /// </summary>
        protected readonly IMaterialRequisitionRepository materialRequisitionRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialRequisitionRepository">物资领用仓储接口</param>
        public ShowMaterialRequisitionCommandHandler(IMaterialRequisitionRepository materialRequisitionRepository)
        {
            this.materialRequisitionRepository = materialRequisitionRepository;
        }

        /// <summary>
        /// 处理显示物资领用命令
        /// </summary>
        /// <param name="request">显示物资领用命令</param>
        /// <param name="cancellationToken">取消标记</param>
        /// <returns>物资领用列表</returns>
        public async Task<ListMaterialRequisitionDto<MaterialRequisition>> Handle(ShowMaterialRequisitionCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await materialRequisitionRepository.GetAllAsync()
                        where !a.IsDeleted 
                        select new MaterialRequisition
                        {
                            MaterialRequisitionId = a.MaterialRequisitionId,
                            CompanyID=a.CompanyID,
                            User_Id=a.User_Id,
                            Title=a.Title,
                            UsageDescription=a.UsageDescription,
                            Recipient=a.Recipient,
                            MaterialRequisitNotes=a.MaterialRequisitNotes,
                            CollectionDate=a.CollectionDate,
                            ApprovalStatus=a.ApprovalStatus,
                            Approver=a.Approver,
                            CreatedBy=a.CreatedBy,
                            CreatedDate=a.CreatedDate,
                            UpdatedBy=a.UpdatedBy,
                            UpdatedDate=a.UpdatedDate,
                            DeletedBy=a.DeletedBy,
                            DeletedDate=a.DeletedDate,
                            IsDeleted=a.IsDeleted
                        };

            if (!string.IsNullOrEmpty(request.Recipient))
            {
                list = list.Where(x => x.Recipient.Contains(request.Recipient));
            }

            if (request.CollectionDate != null)
            {
                list = list.Where(x => x.CollectionDate == request.CollectionDate);
            }

            if (request.ApprovalStatus != null)
            {
                list = list.Where(x => x.ApprovalStatus == request.ApprovalStatus);
            }

            if (!string.IsNullOrEmpty(request.Approver))
            {
                list = list.Where(x => x.Approver.Contains(request.Approver));
            }

            if (request.pageSize == null)
            {
                request.pageSize = list.Count();
            }
            if (request.pageIndex == null)
            {
                request.pageIndex = 1;
            }

            ListMaterialRequisitionDto<MaterialRequisition> res = new ListMaterialRequisitionDto<MaterialRequisition>();
            var index = request.pageIndex;
            var size = request.pageSize;
            res.totalCount = list.Count();
            res.pageCount = (int)Math.Ceiling((res.totalCount * 1.0) / (int)size);
            res.list = list.OrderByDescending(x => x.CreatedDate).Skip(((int)index - 1) * (int)size).Take((int)size).ToList();
            return res;
        }
    }
}
